import { createRouter, createWebHistory } from 'vue-router'
import Admin from '../views/auth/admin/index.vue'
import Group from '../views/auth/group/index.vue'
import Order from '../views/vppz/order/index.vue'
import Staff from '../views/vppz/staff/index.vue'
import Home from '../views/Home.vue'

const routers = localStorage.getItem('Menu')

const routes=[
  //name不能重复
  {
    path: '/',
    name: 'manager',
    meta: { title: "系统管理" },
    component: () => import('../views/Manager.vue'),
    redirect: to=>{
      if(routers){
        //判断是否有子菜单
        const child = JSON.parse(routers).routerList[0].children
        if(child){
          return child[0].meta.path;
        }else{
          return JSON.parse(routers).routerList[0].meta.path;
        }
      }else{
        return '/'
      }
    },
    children: [
      // { path: 'home', name: 'home', meta: { id:"1", title: "主页", icon: "Platform", name:"控制台", path: '/manager/home'}, component: () => import('../views/Home.vue') },
      // { path: 'auth', name: 'auth', meta: { id:"2", title: "权限管理",icon: "Grid", name:"权限管理" },
      //   children:[
      //       { path: '', meta: { id:"1", title: "账号管理", path: '/auth/admin',icon:"Avatar", name:"账号管理" }, component: () => import('../views/auth/admin/index.vue') },
      //       { path: 'group', meta: { id:"2", title: "菜单管理", path: '/auth/group', icon:"Menu", name:"菜单管理"}, component: () => import('../views/auth/group/index.vue') },
      //
      //   ]
      //
      // },
      // { path: 'vppz', name: 'vppz', meta: { id:"3", title: "陪诊页面",icon: "BellFilled",name:"陪诊"},
      //   children:[
      //       { path: '', meta: { id:"1", title: "订单列表", path: '/vppz/order',icon:"DocumentChecked", name:"陪护管理" }, component: () => import('../views/vppz/order/index.vue') },
      //       { path: 'staff', meta: { id:"2", title: "员工信息管理", path: '/vppz/staff', icon:"Document", name:"订单管理"}, component: () => import('../views/vppz/staff/index.vue') },
      //   ]
      // },
    ]
  },
  { path: '/login', name: 'login', meta: { title: "登录页面" }, component: () => import('../views/Login.vue') },
  { path: '/404', name: 'NotFound', meta: { title: "找不到页面" }, component: () => import('../views/404.vue') },
  //路由输错时统一到达 404 页面
  //{ path: '/:pathMatch(.*)', redirect: '/404' },

]
// 创建router,可以实现跳转
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})

//beforeEach 标识跳转前的一些操作
router.beforeEach((to, from, next) => {
  document.title = to.meta.title  //把页面的title修改成meta中定义的
  next()  //必须写
})

export default router
